//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MargrabeOption.h"
using namespace Cephei::QL::Experimental::Exoticoptions;
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/MultiAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (Int32 Q1, Int32 Q2, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : CMultiAssetOption(CMargrabeOption::typeid)
{
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phMargrabeOption = NULL;
#endif
        QuantLib::Integer _Q1 = (QuantLib::Integer)ValueHelper::Convert (Q1); //d
        QuantLib::Integer _Q2 = (QuantLib::Integer)ValueHelper::Convert (Q2); //d
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppMargrabeOption = new boost::shared_ptr<QuantLib::MargrabeOption> (new QuantLib::MargrabeOption ( _Q1,  _Q2,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppMargrabeOption)->setPricingEngine (_QL_Pricer);
        SetMultiAssetOption (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (boost::shared_ptr<QuantLib::MargrabeOption>& childNative, Object^ owner) : CMultiAssetOption(CMargrabeOption::typeid)
{
#ifdef HANDLE
	_phMargrabeOption = NULL;
#endif
	_ppMargrabeOption = &childNative;
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
}
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (QuantLib::MargrabeOption& childNative, Object^ owner) : CMultiAssetOption(CMargrabeOption::typeid)
{
#ifdef HANDLE
	_phMargrabeOption = NULL;
#endif
	_ppMargrabeOption = new boost::shared_ptr<QuantLib::MargrabeOption> (&childNative);
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
    _MargrabeOptionOwner = owner;
    _MultiAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (CMargrabeOption^ copy) : CMultiAssetOption(CMargrabeOption::typeid)
{
#ifdef HANDLE
	_phMargrabeOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMargrabeOption = new boost::shared_ptr<QuantLib::MargrabeOption> (copy->GetShared());
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
    }
}
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (PLATFORM::Type^ t) : CMultiAssetOption(CMargrabeOption::typeid)
{
#ifdef HANDLE
	_phMargrabeOption = NULL;
#endif
	if (!t->IsSubclassOf(CMargrabeOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (QuantLib::Handle<QuantLib::MargrabeOption>& childNative, Object^ owner)  : CMultiAssetOption(CMargrabeOption::typeid)
{
	_phMargrabeOption = &childNative;
	_ppMargrabeOption = &static_cast<boost::shared_ptr<QuantLib::MargrabeOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
    _MargrabeOptionOwner = owner;
}
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (QuantLib::Handle<QuantLib::MargrabeOption> childNative)  : CMultiAssetOption(CMargrabeOption::typeid)
{
	_phMargrabeOption = &childNative;
	_ppMargrabeOption = &static_cast<boost::shared_ptr<QuantLib::MargrabeOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::CMargrabeOption (QuantLib::MargrabeOption childNative)  : CMultiAssetOption(CMargrabeOption::typeid)
{
#ifdef HANDLE
	_phMargrabeOption = NULL;
#endif
	_ppMargrabeOption = new boost::shared_ptr<QuantLib::MargrabeOption> (new QuantLib::MargrabeOption (childNative));
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppMargrabeOption));
}
#endif

Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::~CMargrabeOption ()
{
    if (_ppMargrabeOption != NULL)
    {
	    delete _ppMargrabeOption;
        _ppMargrabeOption = NULL;
    }
}
Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::!CMargrabeOption ()
{
    if (_ppMargrabeOption != NULL)
    {
	    delete _ppMargrabeOption;
    }
}
QuantLib::MargrabeOption& Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::GetReference ()
{
    if (_ppMargrabeOption == NULL) throw REFNEW NativeNullException ();
	return **_ppMargrabeOption;
}
boost::shared_ptr<QuantLib::MargrabeOption>& Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::GetShared ()
{
    if (_ppMargrabeOption == NULL) throw REFNEW NativeNullException ();
	return *_ppMargrabeOption;
}
QuantLib::MargrabeOption* Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::GetPointer ()
{
    if (_ppMargrabeOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppMargrabeOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MargrabeOption>& Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::GetHandle ()
{
	if (_phMargrabeOption == NULL)
	{
		_phMargrabeOption = new Handle<QuantLib::MargrabeOption> (*_ppMargrabeOption);
	}
	return *_phMargrabeOption;
}
#endif
bool Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::HasNative () 
{
	return (_ppMargrabeOption != NULL);
}

Double Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::Delta1::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMargrabeOption)->delta1 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::Delta2::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMargrabeOption)->delta2 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::Gamma1::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMargrabeOption)->gamma1 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Exoticoptions::CMargrabeOption::Gamma2::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMargrabeOption)->gamma2 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Exoticoptions::IMargrabeOption^ Cephei::QL::Experimental::Exoticoptions::CMargrabeOption_Factory::Create (Int32 Q1, Int32 Q2, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CMargrabeOption ( Q1,  Q2,  exercise,  QL_Pricer);
}
